//
//  StatusViewModel.swift
//  微博Swift
//
//  Created by  justinchou on 16/3/1.
//  Copyright © 2016年  justinchou. All rights reserved.
//

import UIKit

// MARK: - 微博cell的布局信息结构体
struct StatusCellViewLayout {
    /// 控件间距
    let margin: CGFloat = 12
    /// 图像间距
    let pictureMargin: CGFloat = 2
    /// 每行图片数量
    let imagesPerRow: CGFloat = 3
    /// 配图视图宽度
    let pictureViewWidth: CGFloat
    /// 配图宽高
    let pictureWH: CGFloat
    
    init() {
        pictureViewWidth = UIScreen.mainScreen().bounds.width - 2 * margin
        pictureWH = (pictureViewWidth - pictureMargin * (imagesPerRow - 1)) / imagesPerRow
    }
    
}

class StatusViewModel: CustomStringConvertible {
    
    // MARK: - 微博来源以及发布时间
    /// 发布时间
    var createTime: NSDate?
    /// 发布来源
    var sourceText: String?
    
    // MARK: - 用户地位等级属性
    /// 会员图像
    var userMemberImage: UIImage?
    /// 认证图像
    var userVerifyImage: UIImage?
    /// 微博模型
    var status: Status
    
    // MARK: - 配图视图
    /// 配图视图大小
    var pictureViewSize = CGSizeZero
    /// cell的布局
    let cellLayout = StatusCellViewLayout()
    /// 被转发微博视图大小
    var retweetedPictureViewSize = CGSizeZero
    
    /// 被转发微博文字
    var retwteedText: String? {
        if status.retweeted_status == nil {
            return nil
        }
        
        return "@" + (status.retweeted_status?.user?.screen_name ?? "") +
            ":" + (status.retweeted_status?.text ?? "")
    }
    
    /// 构造函数
    init(status: Status) {
        
        self.status = status
        
        createUserData()
        createStatusData()
        pictureViewSize = calcPictureViewSize(status.pic_urls?.count ?? 0)
        retweetedPictureViewSize = calcPictureViewSize(status.retweeted_status?.pic_urls?.count ?? 0)

    }
    
    /// 对象描述信息
    var description: String {
        return status.description
    }
    
    // MARK: - 私有方法
    /// 根据配图数量计算配图视图大小
    ///
    /// - returns: 配图视图大小
    private func calcPictureViewSize(count: Int) -> CGSize {
        // 1. 获取配图数量
        if count == 0 {
            return CGSizeZero
        }

        // 2. 根据配图数量计算需要显示行数
        let row = CGFloat((count - 1)/Int(cellLayout.imagesPerRow) + 1)
        
        
        // 3. 根据行数计算配图视图大小
        let height = cellLayout.pictureWH * row + cellLayout.pictureMargin * (row - 1)
    
        return CGSizeMake(cellLayout.pictureViewWidth, height)
    }
    
    
    
    /// 创建微博信息包括微博信息来源以及时间
    private func createStatusData() {
        
        //截取status.source的字符串中的微博来源即可
        if let source = status.source,
            startRange = source.rangeOfString("\">"),
            endRange = source.rangeOfString("</a>") {
                sourceText = "来自 " + source.substringWithRange(startRange.endIndex..<endRange.startIndex)
        }
        
        //设置新浪的日期格式
        createTime = NSDate.justin_sinaDate(status.created_at)
    }
    
    /// 创建会员信息来判断显示何种图片
    private func createUserData() {
        
        //在0～7范围内用不同等级的图片来显示会员
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            userMemberImage = UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        
        // 2. 认证图像
        switch status.user?.verified_type ?? -1 {
        case 0: userVerifyImage = UIImage(named: "avatar_vip")
        case 2, 3, 5: userVerifyImage = UIImage(named: "avatar_enterprise_vip")
        case 220: userVerifyImage = UIImage(named: "avatar_grassroot")
        default: break
        }
        
    }
    
    
}